//
//  YDStatusListViewModel.swift
//  YDWeibo01
//
//  Created by suibian on 16/8/13.
//  Copyright © 2016年 iitcast. All rights reserved.
//

import UIKit

class YDStatusListViewModel: NSObject {
//    保存微博数据的数组
     lazy var dataArray:[YDStatusViewModel] = [YDStatusViewModel]()
    

}
extension YDStatusListViewModel{
//请求首页数据
    
    func loadData(isPullUp:Bool, finish:(issuccess:Bool) -> ()){
        var sinceId:Int64 = 0
        var maxId:Int64 = 0
        
        if isPullUp{
//        上啦加载更多
            maxId = dataArray.last?.statusModel?.id ?? 0
            if maxId > 0{
            maxId -= 1
            
            }
        
        }else{
//        下拉刷新
            sinceId = dataArray.first?.statusModel?.id ?? 0
            
        
        
        }
        YDNetWorkTools.shareTools.homeLoadData(sinceId, maxId: maxId, success: { (response) -> () in
            guard let res = response?["statuses"]  as? [[String:AnyObject]]else{
                finish(issuccess: false)
                return
            }
            
            var tmpArray:[YDStatusViewModel] = [YDStatusViewModel]()
            for dic in res {
                //                获取模型数据
                let status = YDStatusModel(dict: dic)
                let statusModel = YDStatusViewModel(statusModel: status)
                
                
                //                 添加到数组
                tmpArray.append(statusModel)
                
                
            }
            //            赋值
            self.dataArray = tmpArray
//            上拉
            if isPullUp{
            self.dataArray = self.dataArray + tmpArray
            
            }else{
//            下拉刷新
            self.dataArray = tmpArray + self.dataArray
                
            }
            print(self.dataArray.count)
            finish(issuccess: true)
            print("请求成功\(response)")
            


            }) { (error) -> () in
                print("请求失败\(error)")
                finish(issuccess: false)
        }
    
    }
}